package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.vo.Result;
import com.example.entity.Message;
import com.example.entity.PageView;
import com.example.service.IMessageService;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author yzy
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IMessageService iMessageService;

    @Autowired
    private IUserService iUserService;

    // 分页查询+ 前台当前用户信息展示
    @GetMapping("/messageList")
    public Result messageList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                               @RequestParam(value = "content", required = false) String content,
                               @RequestParam(value = "userName", required = false) String userName,
                               @RequestParam(value = "userAccount", required = false) String userAccount,
                               @RequestParam(value = "isRead", required = false) Boolean isRead) {

        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(content)) {
            queryWrapper.like("content", content);
        }
        if (isRead != null) {
            queryWrapper.eq("is_read", isRead);
        }
        if (StringUtils.hasText(userName) || StringUtils.hasText(userAccount)) {
            queryWrapper.or(wrapper -> {
                if (StringUtils.hasText(userName)) {
                    wrapper.like("user.user_name", userName);
                }
                if (StringUtils.hasText(userAccount)) {
                    wrapper.like("user.user_account", userAccount);
                }
            });
        }
        //创建分页对象
        Page<Message> page = new Page<>(pageNum, pageSize);
        //执行分页查询
        Page<Message> page1 = iMessageService.selectMessageList(page, queryWrapper);
        //返回结果
        PageView pageView = new PageView();
        pageView.setPageSize(Integer.parseInt(page1.getSize() + ""));
        pageView.setPageNum(Integer.parseInt(page1.getCurrent() + ""));
        pageView.setAllRow(page1.getTotal());
        pageView.setTotalPage(page1.getPages());
        pageView.setData(page1.getRecords());
        return Result.success(pageView);
    }

    //根据消息id查看消息
    @GetMapping("/getMessageById/{id}")
    public Result getMessageById(@PathVariable("id") Integer id) {
        Message byId = this.iMessageService.getById(id);
        System.out.println("byId = " + byId);
        return Result.success(byId);
    }

    //增加消息
    @PostMapping("/addMessage")
    public Result addMessage(@RequestBody Message message) {
        message.setIsRead(false);
        message.setCreateTime(LocalDateTime.now());
        this.iMessageService.saveOrUpdate(message);
        return Result.success(null);
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        this.iMessageService.removeById(id);
        return Result.success(null);
    }

    //前台用户已读消息
    @PostMapping("/MessageIsRead")
    public Result MessageIsRead(@RequestBody Message message) {
        message.setIsRead(true);
        message.setCreateTime(LocalDateTime.now());
         this.iMessageService.updateById(message);
        System.out.println("message = " + message);

        return Result.success(null);
    }



}
